
import de.uniaugsburg.swt.javacard.terminalinterface.SWTReader;
import de.uniaugsburg.swt.javacard.terminalinterface.SWTTerminalInterface;
import javax.crypto.*;
import javax.crypto.spec.*;
import com.ibm.opencard.terminal.pcsc10.PcscException;



/**
 * TerminalLoyaltyCard.java
 *
 * Created on 27. November 2007, 21:31
 *
 * @author  Wei Zhao
 */

public class TerminalLoyaltyCard extends javax.swing.JFrame {

    private static SWTReader reader;
    private static SWTTerminalInterface sc;
    private static Cipher cipPartner;
    private static Cipher cipLC;
    private static String val = "";
    private static SecretKey keyPa;
    private static SecretKey keyFi;
    private int laden =0 ;
    private static byte[] keyP  = new byte [24];
    /* {
      (byte)0x77, (byte)0x60, (byte)0xA5, (byte)0xE2,
      (byte)0x21, (byte)0x65, (byte)0x06, (byte)0xAE,
      (byte)0x15, (byte)0x21, (byte)0x80, (byte)0xB4,
      (byte)0xC9, (byte)0x36, (byte)0x22, (byte)0x25,
      (byte)0xB4, (byte)0x46, (byte)0xD5, (byte)0x2D,
      (byte)0x9C, (byte)0x57, (byte)0x56, (byte)0x41
      };*/
  private static  byte[] keyL  = new byte [24];
   /*  {
      (byte)0x71, (byte)0xA0, (byte)0xE5, (byte)0xE0,
      (byte)0x35, (byte)0xB5, (byte)0x46, (byte)0xCE,
      (byte)0x41, (byte)0x6C, (byte)0x53, (byte)0xB5,
      (byte)0x62, (byte)0x16, (byte)0x02, (byte)0x20,
      (byte)0x10, (byte)0x16, (byte)0xD5, (byte)0x6D,
      (byte)0x55, (byte)0x22, (byte)0x38, (byte)0xA5
      };*/
    private static byte select[] = {0x00, (byte) 0xA4, 0x04, 0x00, 0x06, 0x73,
        0x77, 0x74, 0x00, (byte) 0xA3, 0x00
    };
    private static byte leistung[] = {(byte) 0x90, 0x00, 0x00, 0x00, 0x04, 0x00,
        0x00, 0x00, 0x00, 0x02
    };
    byte[] secretlist = {42, 21, 11, 4, 7, 100, 104, 33};
    byte auth[] = {(byte) 0x90, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02
    };
    private byte store=0;
  
    public TerminalLoyaltyCard() {
       // initSecrect();
        initComponents();


    }
/**
 * initialization of secret Key 
 */
    private void initSecrect() {

   
        try {
            SecretKeyFactory sessionkf1 = SecretKeyFactory.getInstance("DESede");
            keyPa = sessionkf1.generateSecret(new DESedeKeySpec(keyP));
            keyFi = sessionkf1.generateSecret(new DESedeKeySpec(keyL));
            cipPartner = Cipher.getInstance("DESede/ECB/NoPadding");
            cipLC = Cipher.getInstance("DESede/ECB/NoPadding");
            cipPartner.init(javax.crypto.Cipher.ENCRYPT_MODE, keyPa);
            cipLC.init(javax.crypto.Cipher.ENCRYPT_MODE, keyFi);
             
        /* Debug  
        byte[] secret2=cipLC.doFinal(secret);
        cipLC.init(javax.crypto.Cipher.DECRYPT_MODE, keyFi);
        byte[] secret3=cipLC.doFinal(secret2);
        for (byte i =0 ;i<24;i++){
        System.out.println(secret2[i]);
        System.out.println(secret3[i]);        }*/
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

/**
 * initialization of Components on User interface
 */                      
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        kundenPanel = new javax.swing.JPanel();
        checkPunkt1 = new javax.swing.JButton();
        punktField1 = new javax.swing.JTextField();
        partnerPanel = new javax.swing.JPanel();
        punktField2 = new javax.swing.JTextField();
        checkPunkt2 = new javax.swing.JButton();
        punktField9 = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jRadioButton3 = new javax.swing.JRadioButton();
        jRadioButton4 = new javax.swing.JRadioButton();
        firmaPanel = new javax.swing.JPanel();
        checkPunkt3 = new javax.swing.JButton();
        punktField3 = new javax.swing.JTextField();
        punktField4 = new javax.swing.JTextField();
        punktField5 = new javax.swing.JTextField();
        punktField6 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        punktField7 = new javax.swing.JTextField();
        punktField8 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        checkPunkt1.setText("Check Punkt");
        checkPunkt1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                checkPunkt1ActionPerformed(evt);
            }
        });

        punktField1.setEditable(false);
        punktField1.setFont(new java.awt.Font("Arial", 1, 18));

        javax.swing.GroupLayout kundenPanelLayout = new javax.swing.GroupLayout(kundenPanel);
        kundenPanel.setLayout(kundenPanelLayout);
        kundenPanelLayout.setHorizontalGroup(
            kundenPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(kundenPanelLayout.createSequentialGroup()
                .addGap(73, 73, 73)
                .addComponent(checkPunkt1)
                .addGap(49, 49, 49)
                .addComponent(punktField1, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(370, Short.MAX_VALUE))
        );
        kundenPanelLayout.setVerticalGroup(
            kundenPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(kundenPanelLayout.createSequentialGroup()
                .addGap(158, 158, 158)
                .addGroup(kundenPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(checkPunkt1)
                    .addComponent(punktField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(263, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Kunden Terminal", kundenPanel);

        punktField2.setEditable(false);
        punktField2.setFont(new java.awt.Font("Arial", 1, 18));

        checkPunkt2.setText("Check Punkt");
        checkPunkt2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                checkPunkt2ActionPerformed(evt);
            }
        });

        punktField9.setFont(new java.awt.Font("Arial", 1, 18));

        jButton2.setText("laden ");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jRadioButton1.setText("SuperSizeMe");
        jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton1ActionPerformed(evt);
            }
        });

        jRadioButton2.setText("AllForMore");
        jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton2ActionPerformed(evt);
            }
        });

        jRadioButton3.setText("LongAndBoring");
        jRadioButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton3ActionPerformed(evt);
            }
        });

        jRadioButton4.setText("HippoWear");
        jRadioButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout partnerPanelLayout = new javax.swing.GroupLayout(partnerPanel);
        partnerPanel.setLayout(partnerPanelLayout);
        partnerPanelLayout.setHorizontalGroup(
            partnerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(partnerPanelLayout.createSequentialGroup()
                .addGroup(partnerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(partnerPanelLayout.createSequentialGroup()
                        .addGap(81, 81, 81)
                        .addComponent(jRadioButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jRadioButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jRadioButton3)
                        .addGap(18, 18, 18)
                        .addComponent(jRadioButton4))
                    .addGroup(partnerPanelLayout.createSequentialGroup()
                        .addGap(140, 140, 140)
                        .addGroup(partnerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(partnerPanelLayout.createSequentialGroup()
                                .addComponent(checkPunkt2, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(35, 35, 35)
                                .addComponent(punktField2, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(partnerPanelLayout.createSequentialGroup()
                                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(37, 37, 37)
                                .addComponent(punktField9, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addGap(277, 277, 277))
        );
        partnerPanelLayout.setVerticalGroup(
            partnerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(partnerPanelLayout.createSequentialGroup()
                .addGap(82, 82, 82)
                .addGroup(partnerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(checkPunkt2)
                    .addComponent(punktField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(72, 72, 72)
                .addGroup(partnerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jRadioButton1)
                    .addComponent(jRadioButton2)
                    .addComponent(jRadioButton3)
                    .addComponent(jRadioButton4))
                .addGap(18, 18, 18)
                .addGroup(partnerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(punktField9, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(198, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Partner Terminal", partnerPanel);

        checkPunkt3.setText("Check gesamte Punkte");
        checkPunkt3.addActionListener(new java.awt.event.ActionListener() {
      
        	public void actionPerformed(java.awt.event.ActionEvent evt) {
                checkPunkt3ActionPerformed(evt);
            }
        });

        punktField3.setEditable(false);
        punktField3.setFont(new java.awt.Font("Arial", 1, 18));

        punktField4.setEditable(false);
        punktField4.setFont(new java.awt.Font("Arial", 1, 18));

        punktField5.setEditable(false);
        punktField5.setFont(new java.awt.Font("Arial", 1, 18));

        punktField6.setEditable(false);
        punktField6.setFont(new java.awt.Font("Arial", 1, 18));

        jButton1.setText("Entladen ");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        punktField7.setEditable(false);
        punktField7.setFont(new java.awt.Font("Arial", 1, 18));

        punktField8.setFont(new java.awt.Font("Arial", 1, 18));

        jLabel1.setText("SuperSizeMe");

        jLabel2.setText("AllForMore");

        jLabel3.setText("LongAndBoring");

        jLabel4.setText("HippoWear");

        javax.swing.GroupLayout firmaPanelLayout = new javax.swing.GroupLayout(firmaPanel);
        firmaPanel.setLayout(firmaPanelLayout);
        firmaPanelLayout.setHorizontalGroup(
            firmaPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(firmaPanelLayout.createSequentialGroup()
                .addGroup(firmaPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(firmaPanelLayout.createSequentialGroup()
                        .addGap(117, 117, 117)
                        .addGroup(firmaPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(checkPunkt3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 42, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, firmaPanelLayout.createSequentialGroup()
                        .addGap(202, 202, 202)
                        .addGroup(firmaPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2))
                        .addGap(43, 43, 43)))
                .addGroup(firmaPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(punktField6, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(firmaPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(firmaPanelLayout.createSequentialGroup()
                            .addComponent(punktField7, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, firmaPanelLayout.createSequentialGroup()
                            .addGroup(firmaPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(punktField3, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(punktField4, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(punktField5, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGap(144, 144, 144)))
                    .addComponent(punktField8, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(432, 432, 432))
        );
        firmaPanelLayout.setVerticalGroup(
            firmaPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(firmaPanelLayout.createSequentialGroup()
                .addGap(44, 44, 44)
                .addGroup(firmaPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(checkPunkt3)
                    .addComponent(punktField7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(firmaPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(punktField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(firmaPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(punktField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(firmaPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(punktField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(firmaPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(punktField6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(77, 77, 77)
                .addGroup(firmaPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(punktField8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(124, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("LoyaltyCard", firmaPanel);

        getContentPane().add(jTabbedPane1, java.awt.BorderLayout.CENTER);

        pack();
    }
    /**
     * check points of corporation 
     * 
     */                       
    private void checkPunkt3ActionPerformed(java.awt.event.ActionEvent evt) {                                            
        try {
             leistung[1] = 0x10;
             leistung[9] = 10;
            byte[] p = reader.transmit(leistung);
            cipLC.init(javax.crypto.Cipher.DECRYPT_MODE, keyFi);
            p = cipLC.doFinal(p);

            val = String.valueOf(((p[0] & 0xff) << 8) | (p[1] & 0xff));
            punktField7.setText(val);

            val = String.valueOf(((p[2] & 0xff) << 8) | (p[3] & 0xff));
            punktField3.setText(val);
            val = String.valueOf(((p[4] & 0xff) << 8) | (p[5] & 0xff));
            punktField4.setText(val);
            val = String.valueOf(((p[6] & 0xff) << 8) | (p[7] & 0xff));
            punktField5.setText(val);
            val = String.valueOf(((p[8] & 0xff) << 8) | (p[9] & 0xff));
            punktField6.setText(val);
        } catch (Exception e) {
            e.printStackTrace();
        }
         
    }                                           
    /**
     * decrease points 
     * 
     */
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        short entladenv = Short.parseShort(punktField8.getText().trim());
       
        try {
            cipLC.init(javax.crypto.Cipher.ENCRYPT_MODE, keyFi);
            byte[] temp = cipLC.doFinal(secretlist);
            
            for (int i = 0; i < temp.length; i++) {
                auth[i + 5] = temp[i];
            }
            auth[1] = 0x06;            
            auth[auth.length - 4] = 0x02;
            auth[auth.length - 3] = (byte) ((entladenv >> 8) & 0xff);
            auth[auth.length - 2] = (byte) (entladenv & 0xff);
            auth[auth.length - 1] = 10;
            byte[] allValue = reader.transmit(auth);
            
            countMinus(allValue,entladenv);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }                                        

    
    private void countMinus(byte[] allValue,short entladenv) {
		float value =(short)( allValue[0]<<8|allValue[1]);
		float SuperSizeMeCount = (short)( allValue[2]<<8|allValue[3]);	
		float AllForMoreCount = (short)( allValue[4]<<8|allValue[5]);
		float LongAndBoringCount = (short)( allValue[6]<<8|allValue[7]);
		float HippoWearCount = (short)( allValue[8]<<8|allValue[9]);
		SuperSizeMeCount -= entladenv*(SuperSizeMeCount/value);
		AllForMoreCount -= entladenv*(AllForMoreCount/value);
		LongAndBoringCount -= entladenv*(LongAndBoringCount/value);
		HippoWearCount -= entladenv*(HippoWearCount/value);
		short ss= (short)SuperSizeMeCount;
		short af= (short)AllForMoreCount;
		short la = (short )LongAndBoringCount;
		short hw = (short )HippoWearCount;
		auth[1] = 0x12; 
		auth[5]=(byte)(ss>>8);
		auth[6]=(byte)(ss);
		auth[7]=(byte)(af>>8);
		auth[8]=(byte)(af);
		auth[9]=(byte)(la>>8);
		auth[10]=(byte)(la);
		auth[11]=(byte)(hw>>8);
		auth[12]=(byte)(hw);
		auth[auth.length - 1] = 0;
		try {
			reader.transmit(auth);
		} catch (PcscException e) {
			e.printStackTrace();
		}
		
		
		
	}

    /**
     * check all points of client 
     * 
     */
	private void checkPunkt2ActionPerformed(java.awt.event.ActionEvent evt) {                                            
        leistung[1] = 0x08;
        leistung[9] = 2;
        try {
            byte[] p = reader.transmit(leistung);
            val = String.valueOf(((p[0] & 0xff) << 8) | (p[1] & 0xff));
            punktField2.setText(val);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }                                           
	 /**
	  *  add new points
	  * 
	  */
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         

    	if (punktField9.getText().trim()!="" && laden!=0){
        short ladenv = Short.parseShort(punktField9.getText().trim());
        
        try {
        	
            cipPartner.init(javax.crypto.Cipher.ENCRYPT_MODE, keyPa);
            byte[] temp = cipPartner.doFinal(secretlist);
            for (int i = 0; i < temp.length; i++) {
                auth[i + 5] = temp[i];
            }
            auth[1] = 0x04;
            auth[auth.length - 5] = store;
            auth[auth.length - 4] = 0x01;
            auth[auth.length - 3] = (byte) ((ladenv >> 8) & 0xff);
            auth[auth.length - 2] = (byte) (ladenv & 0xff);
            auth[auth.length - 1] = 2;
           reader.transmit(auth);
        	  
        } catch (Exception e) {
            e.printStackTrace();
        }}
    }                                        
 /**
  * check all points of client 
  * 
  */
    private void checkPunkt1ActionPerformed(java.awt.event.ActionEvent evt) {                                            
        try {

            leistung[1] = 0x08;
            leistung[9] = 2;

            byte[] p = reader.transmit(leistung);
            val = String.valueOf(((p[0] & 0xff) << 8) | (p[1] & 0xff));
        } catch (Exception e) {
            e.printStackTrace();
        }

        punktField1.setText(val); 
    }                                           

    private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                              
                  store = 0x22;
                  jRadioButton4.setSelected(false);
                  jRadioButton2.setSelected(true);
                  jRadioButton1.setSelected(false);
                  jRadioButton3.setSelected(false);
                  laden = 1;
    }                                             

    private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                              
                  store = 0x20;
                  jRadioButton4.setSelected(false);
                  jRadioButton2.setSelected(false);
                  jRadioButton1.setSelected(true);
                  jRadioButton3.setSelected(false);
                  laden = 1;
    }                                             

    private void jRadioButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                              
                  store = 0x24;
                  jRadioButton4.setSelected(false);
                  jRadioButton2.setSelected(false);
                  jRadioButton1.setSelected(false);
                  jRadioButton3.setSelected(true);
                  laden = 1;
    }                                             

    private void jRadioButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                              
                  store = 0x26;
                  jRadioButton4.setSelected(true);
                  jRadioButton2.setSelected(false);
                  jRadioButton1.setSelected(false);
                  jRadioButton3.setSelected(false);
                  laden = 1;
                    
    }                                             

    /**
     *  start the  Terminal Interface
     */
    public static void main(String args[]) {
        try {
            sc = new SWTTerminalInterface();
            String list[] = sc.getReaderNames();
            reader = sc.getReader(list[0]);
            reader.connect();
            reader.transmit(select);
            leistung [1]= 0x14;
            leistung [leistung.length-1]=0x30;
            
            byte [] keys = reader.transmit(leistung);
            
            for (int m  = 0 ;m <48 ; m++){
            	
            if  (m<=23)	keyP[m] = keys[m];
            if  (m>23)  keyL[m-24] = keys[m];
            	
            }
            TerminalLoyaltyCard TL=  new TerminalLoyaltyCard();
            TL.initSecrect();
            TL.setVisible(true);
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
               // new TerminalLoyaltyCard().setVisible(true);
            }
        });
    }
                        
    private javax.swing.JButton checkPunkt1;
    private javax.swing.JButton checkPunkt2;
    private javax.swing.JButton checkPunkt3;
    private javax.swing.JPanel firmaPanel;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JRadioButton jRadioButton3;
    private javax.swing.JRadioButton jRadioButton4;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JPanel kundenPanel;
    private javax.swing.JPanel partnerPanel;
    private javax.swing.JTextField punktField1;
    private javax.swing.JTextField punktField2;
    private javax.swing.JTextField punktField3;
    private javax.swing.JTextField punktField4;
    private javax.swing.JTextField punktField5;
    private javax.swing.JTextField punktField6;
    private javax.swing.JTextField punktField7;
    private javax.swing.JTextField punktField8;
    private javax.swing.JTextField punktField9;
    // End of variables declaration                   
}
